<?php
/**
 * Created by PhpStorm.
 * User: ZNZG
 * Date: 2020/3/19
 * Time: 19:59
 */

namespace app\common\lib;

use think\helper\Str as StrHelper;

class Str
{
    /**
     * 格式化密码（将生成加密后的密码及加密字符返回）
     * @param $password
     * @return array
     */
    public static function formatPassword($password)
    {
        $salt = StrHelper::random(6);
        $password = self::encryptPassword($salt, $password);

        return [
            'salt' => $salt,
            'password' => $password
        ];
    }

    /**
     * 生成加密后的密码
     * @param $salt
     * @param $password
     * @return string
     */
    public static function encryptPassword($salt, $password)
    {
        return md5(sha1($password) . $salt);
    }

    /**
     * 生成订单号
     * @param string $prefix 标识支付途径 如 W 微信  A 支付宝
     * @return string
     */
    public static function createOrder($prefix = '')
    {
        $prefix = substr($prefix, 0, 1);
        $dateStr = date('YmdHis');
        $msectime = self::msectime();
        $randStr = rand(10000, 99999);

        return substr($prefix . $dateStr . $msectime . $randStr, 0, 32);
    }

    /**
     * @return float
     */
    public static function msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }

    /**
     * 更改反斜杠为正斜杠
     * @param $path
     * @return mixed
     */
    public static function formatPath($path)
    {
        return str_replace('\\', '/', $path);
    }

    /**
     * 替换单引号为双引号
     * @param $str
     * @return mixed
     */
    public static function replaceSingleQuotToDoubleQuot($str)
    {
        return str_replace('\'', '"', $str);
    }

    /**
     * 替换双引号为单引号
     * @param $str
     * @return mixed
     */
    public static function replaceDoubleQuotToSingleQuot($str)
    {
        return str_replace('"', '\'', $str);
    }

    /**
     * 替换中文逗号为英文逗号
     * @param $str
     * @return mixed
     */
    public static function replaceSepToEn($str)
    {
        return str_replace('，', ',', $str);
    }

    /**
     * 生成订单号
     * @param string $types
     * @return string
     */
    public static function generateTradeNo($types = 'W')
    {
        $result = $types . date('YmdHis') . rand(1000, 9999);
        return $result;
    }

    /**
     * 随机生成8位数字
     * @param int $start
     * @param int $end
     * @param int $length
     * @return mixed
     */
    public static function getRandNumber($start = 0, $end = 9, $length = 8)
    {
        $connt = 0;
        $temp = array();
        while ($connt < $length) {
            $temp[] = mt_rand($start, $end);
            $data = array_flip(array_flip($temp));
            $connt = count($data);
        }
        shuffle($data);
        $str = implode(",", $data);
        $number = str_replace(',', '', $str);

        return $number;
    }

    /**
     * 根据毫秒时间戳生成随机数
     * @param int $length
     * @return string
     */
    public static function getRandNumberByMicrotime($length = 9)
    {
        $arr = explode('.', microtime(true));
        $arr_l = $arr[0];
        $arr_r = $arr[1];
        $arr_l_length = strlen($arr_l);
        if ($length >= 5) {
            return substr($arr_l, $arr_l_length - ($length - 4)) . str_pad($arr_r, 4, STR_PAD_LEFT);
        } else {
            return substr($arr_l, $arr_l_length - $length);
        }
    }
}